<?php

namespace IDock\Insurance\ACIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\ACIC\ACICEnums;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumRelationPrinter;
use IDock\Printer\RenewalInquiryPrinter;
use IDock\Printer\VehiclePrinter;

class RenewalInquiryResponse extends AllACICResponse
{
    public function resolve(): Response
    {
        $body = $this->getBody();

        if ($body) {
            libxml_disable_entity_loader(false);

            $body = simplexml_load_string($body);

            $body = $this->objectToArray($body);

            $errorCode = $body['Head']['ErrorCode'] ?? '';
            $responseCode = $body['Head']['ResponseCode'] ?? '';

            if ($errorCode !== '0000') {
                if ($errorCode == '6056' and $responseCode == '-1') {
                    $this->setBody($responseCode);
                    return $this;
                }

                $this->setError($body['Head']['ErrorMessage'] ?? '未知错误');
                return $this;
            }

            $this->setBody($body['Body'] ?? '');
        }

        return $this;
    }

    public function printerInit(): Response
    {
        $renewalInquiryPrinter = new RenewalInquiryPrinter();

        if ($this->get('ResultFlag')) {
            $renewalInquiryPrinter->setRenewalFlg(true);
        } else {
            $this->printer = $renewalInquiryPrinter;
            return $this;
        }

        $policyList = $this->get('Policy', []);

        foreach ($policyList as $policyItem) {
            $applicantPrinter = new PremiumRelationPrinter();
            $insuredPrinter = new PremiumRelationPrinter();
            $ownerPrinter = new PremiumRelationPrinter();
            $vehiclePrinter = new VehiclePrinter();

            //车牌号
            $renewalInquiryPrinter->licenseNo = $vehicle['LicensePlateNo'] ?? '';
            //车架号
            $renewalInquiryPrinter->vin = $vehicle['VIN'] ?? '';
            //发动机号
            $renewalInquiryPrinter->engineNo = $vehicle['EngineNo'] ?? '';

            //投保人
            if ($applicant = $policyItem['Applicant'] ?? []) {
                $applicantPrinter->name = $applicant['Name'] ?? '';
                $applicantPrinter->mobile = $applicant['Phone'] ?? '';
                $applicantPrinter->address = $applicant['Address'] ?? '';
                $applicantPrinter->certificateNo = $applicant['CertificateNo'] ?? '';
                $applicantPrinter->certificateTypeCode = array_keys(ACICEnums::$certificateTypeCode, $applicant['CertificateType'] ?? '')[0] ?? '';
                $applicantPrinter->email = $applicant['Email'] ?? '';
                $applicantPrinter->certificateStartDate = $applicant['IdBegDate'] ?? '';
                $applicantPrinter->certificateEndDate = $applicant['IdEndDate'] ?? '';

                $applicantPrinter->typeCode = $applicantPrinter->certificateTypeCode == '01' ? '01' : '03';

            }

            //被保人
            if ($insured = $policyItem['Insured'] ?? []) {
                $insuredPrinter->name = $insured['Name'] ?? '';
                $insuredPrinter->mobile = $insured['Phone'] ?? '';
                $insuredPrinter->address = $insured['Address'] ?? '';
                $insuredPrinter->certificateNo = $insured['CertificateNo'] ?? '';
                $insuredPrinter->certificateTypeCode = array_keys(ACICEnums::$certificateTypeCode, $insured['CertificateType'] ?? '')[0] ?? '';
                $insuredPrinter->email = $insured['Email'] ?? '';
                $insuredPrinter->certificateStartDate = $insured['IdBegDate'] ?? '';
                $insuredPrinter->certificateEndDate = $insured['IdEndDate'] ?? '';

                $insuredPrinter->typeCode = $insuredPrinter->certificateTypeCode == '01' ? '01' : '03';
            }

            //车主
            if ($owner = $policyItem['Owner'] ?? []) {
                $ownerPrinter->name = $owner['Name'] ?? '';
                $ownerPrinter->mobile = $owner['Phone'] ?? '';
                $ownerPrinter->address = $owner['Address'] ?? '';
                $ownerPrinter->certificateNo = $owner['CertificateNo'] ?? '';
                $ownerPrinter->certificateTypeCode = array_keys(ACICEnums::$certificateTypeCode, $owner['CertificateType'] ?? '')[0] ?? '';
                $ownerPrinter->email = $owner['Email'] ?? '';
                $ownerPrinter->certificateStartDate = $owner['IdBegDate'] ?? '';
                $ownerPrinter->certificateEndDate = $owner['IdEndDate'] ?? '';

                $ownerPrinter->typeCode = $ownerPrinter->certificateTypeCode == '01' ? '01' : '03';
            }

            //车辆信息
            if ($vehicle = $policyItem['Vehicle'] ?? []) {
                //初等时间
                $renewalInquiryPrinter->firstRegisterDate = $vehicle['FirstRegisterDate'] ??'';

                $vehiclePrinter->vehicleName = $vehicle['CarName'] ?? '';
                $vehiclePrinter->brandName = $vehicle['BrandName'] ?? '';
                $vehiclePrinter->familyName = $vehicle['FamilyName'] ?? '';
                $vehiclePrinter->power = $vehicle['Power'] ?? '';
                $vehiclePrinter->vehiclePrice = $vehicle['ReplacementValue'] ?? '';
                $vehiclePrinter->vehiclePriceTax = $vehicle['ReplacementValue'] ?? '';
                $vehiclePrinter->vehicleExhaust = $vehicle['DisplaceMentEx'] ?? '';
                $vehiclePrinter->vehicleSeat = $vehicle['CSeat'] ?? '';
                $vehiclePrinter->vehicleYear = $vehicle['VhlYear'] ?? '';
                $vehiclePrinter->vehicleTonnage = $vehicle['Tonnage'] ?? '';
                $vehiclePrinter->wholeWeight = $vehicle['Whole_Weight'] ?? '';

                //进口标识
                $vehicleType = $vehicle['IpmORLoc'] ?? 'I0' ?: 0;

                $vehiclePrinter->vehicleImport = ACICEnums::$importFlag[$vehicleType];
                $vehiclePrinter->vehicleImportName = IDockEnums::$importFlag[$vehiclePrinter->vehicleImport];
            }


            //保单信息
            if ($application = $policyItem['Application'] ?? []) {
                $policyType = $application['ProdNo'] ?? '';

                //交强险
                if ($policyType == '0330') {
                    $renewalInquiryPrinter->ciEndData = ($application['ExpireDate'] ?? '') ? date('Y-m-d H:i:s', strtotime($application['ExpireDate'])) : '';
                    $renewalInquiryPrinter->ciPolicyNo = $application['LastPolicyNo'] ?? '';

                    if ($renewalInquiryPrinter->ciEndData and date('H:i:s',strtotime($renewalInquiryPrinter->ciEndData)) == '23:59:59') {
                        $renewalInquiryPrinter->ciEndData = date('Y-m-d H:i:s',strtotime('+1 second',strtotime($renewalInquiryPrinter->ciEndData)));
                    }
                }

                //商业险
                if ($policyType == '0333') {
                    $renewalInquiryPrinter->biEndData = ($application['ExpireDate'] ?? '') ? date('Y-m-d H:i:s', strtotime($application['ExpireDate'])) : '';
                    if ($renewalInquiryPrinter->biEndData and  date('H:i:s',strtotime($renewalInquiryPrinter->biEndData)) == '23:59:59') {
                        $renewalInquiryPrinter->biEndData = date('Y-m-d H:i:s',strtotime('+1 second',strtotime($renewalInquiryPrinter->biEndData)));
                    }
                    $renewalInquiryPrinter->biPolicyNo = $application['LastPolicyNo'] ?? '';

                    //商业险险种
                    if ($coverageList = $policyItem['CoverageList']['CoverageItem'] ?? []) {
                        foreach ($coverageList as $coverage) {
                            $coveragePrinter = new PremiumCoveragePrinter();

                            $coveragePrinter->coverageCode = array_search($coverage['CoverageCode'], ACICEnums::$coverageCode);

                            if (!$coveragePrinter->coverageCode) {
                                continue;
                            }

                            $coveragePrinter->coverageName = IDockEnums::$coverageCode[$coveragePrinter->coverageCode];
                            $coveragePrinter->amount = (string)(int)$coverage['SumLimit'] ?? '';
                            $coveragePrinter->unitAmount = (string)(int)$coverage['DayFee'] ?? '';
                            $coveragePrinter->quantity = $coverage['DaySeatNum'] ?? '';

                            $renewalInquiryPrinter->setCoveragePrinterList($coveragePrinter);
                        }
                    }
                }
            }

            $renewalInquiryPrinter->setApplicantPrinter($applicantPrinter);
            $renewalInquiryPrinter->setInsuredPrinter($insuredPrinter);
            $renewalInquiryPrinter->setOwnerPrinter($ownerPrinter);
            $renewalInquiryPrinter->setVehiclePrinter($vehiclePrinter);

        }

        $this->printer = $renewalInquiryPrinter;

        return $this;
    }
}